package com.baizan.daka.ui.activity

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import com.baizan.base.BaseActivity
import com.baizan.daka.base.BasePresenter
import com.baizan.daka.mvp.model.bean.BankCardInfoBean
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.ShowCashDesContract
import com.baizan.daka.ui.presenter.ShowCashDesPresenter
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_showcash_des.*
import kotlinx.android.synthetic.main.act_showcash_des.bank_title
import kotlinx.android.synthetic.main.act_showcash_des.password
import kotlinx.android.synthetic.main.act_showcash_des.submit_bt

class ShowCashDesActivity : BaseActivity<ShowCashDesPresenter>(), ShowCashDesContract.View {

    companion object {
        fun start(context: Context, money: Double, bankCardInfoBean: BankCardInfoBean) {
            val intent = Intent(context, ShowCashDesActivity::class.java)
            intent.putExtra("money", money)
            intent.putExtra("bank", bankCardInfoBean)
            context.startActivity(intent)
        }
    }

    override fun initPresenter(): ShowCashDesPresenter = ShowCashDesPresenter(this)

    override fun layoutId(): Int = R.layout.act_showcash_des

    override fun initData() {
        val money = intent.getDoubleExtra("money", 0.00)
        val bank = intent.getSerializableExtra("bank")
        bank_title.text = "单日提现限额50000.00元；可提现金额${money}元"
        bank?.let {
            it as BankCardInfoBean
            initBank(it, money)

        }
    }

    private fun initBank(it: BankCardInfoBean, maxMoney: Double) {
        var lastCode: String? = null
        lastCode = if (it.cardNo.length > 4) it.cardNo.takeLast(4) else it.cardNo
        bank_no.text = "${it.bankName}(尾号${lastCode})"
        money_tv.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!s.isNullOrEmpty()) {
                    try {
                        val inputMoney = s.toString().toDouble()
                        if (inputMoney > maxMoney) {
                            money_tv.setText(s.take(s.length - 1))
                        }
                    }catch (e:NumberFormatException){
                        money_tv.setText(s.take(s.length - 1))
                    }

                }

            }
        })
        submit_bt.setOnClickListener {

            var passwordValue = password.text
            if (passwordValue.isEmpty()) {
                showToast("请输资金密码")
                return@setOnClickListener
            }
            var money = money_tv.text
            if (money.isEmpty() and (money.toString().toDouble()<50)) {
                showToast("单笔提现金额需要大于50元")
                return@setOnClickListener
            }
            presenter?.cashOut(money_tv.text.toString(),passwordValue.toString())
        }
    }

    override fun initView() {
    }

    override fun start() {

    }

    override fun cashOutSuccess() {
        showToast("提现成功")
        finish()
    }

}